﻿using dateCopy.Models;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace dateCopy
{
    class Program
    {
        static void Main(string[] args)
        {
            string conStr = "Data Source=117.121.26.159, 1433;Persist Security Info=True;User ID=sa;Password=Seuic123;Initial Catalog=TMS";
            List<TMSUpdate> ls = new List<TMSUpdate>();
            SqlConnection Conn = new SqlConnection(conStr);
            string today = DateTime.Now.ToString("yyyyMMdd");
            //string SQLstring = string.Format("select * from TmsUpdate where (convert(int,convert(varchar(10),UpdateTime,112)) ={0})",today);
            string SQLstring = string.Format("select * from TmsUpdate where (convert(int,convert(varchar(10),UpdateTime,112)) ={0})", DateTime.Now.AddDays(-1).ToString("yyyyMMdd"));
            SqlCommand Cmd = new SqlCommand(SQLstring, Conn);
            try
            {
                Conn.Open();
                SqlDataReader sdr = Cmd.ExecuteReader();
                while (sdr.Read())
                {
                    Console.WriteLine("数据导出成功");
                    TMSUpdate tms = new TMSUpdate();
                    tms.UpdateTime = sdr["UpdateTime"].ToString().Trim();
                    tms.TermId = sdr["TermId"].ToString().Trim();
                    tms.TermVersion = sdr["TermVersion"].ToString().Trim();
                    tms.NowVersion = sdr["NowVersion"].ToString().Trim();
                    tms.RespCode = sdr["RespCode"].ToString().Trim();
                    tms.Result = sdr["Result"].ToString().Trim();
                    tms.TermType = sdr["TermType"].ToString().Trim();
                    tms.HardType = sdr["HardType"].ToString().Trim();
                    ls.Add(tms);
                }

            }
            catch (System.Data.SqlClient.SqlException E)
            {
                throw new Exception(E.Message);
            }
            finally
            {
                Cmd.Dispose();
                Conn.Close();
            }



            string conStr1 = "Data Source=192.168.10.10, 1433;Persist Security Info=True;User ID=sa;Password=Seuic123;Initial Catalog=TMS";
            SqlConnection Conn1 = new SqlConnection(conStr1);
            try
            {
                Conn1.Open();
                for (var i = 0; i < ls.Count; i++)
                {
                    string SQLstring1 = string.Format("INSERT INTO TmsUpdate VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')", ls[i].UpdateTime, ls[i].TermId, ls[i].TermVersion, ls[i].NowVersion, ls[i].RespCode, ls[i].Result, ls[i].TermType, ls[i].HardType);
                    SqlCommand Cmd1 = new SqlCommand(SQLstring1, Conn1);
                    Cmd1.ExecuteNonQuery();
                    Console.WriteLine("第" + i + "条数据导入成功");
                }

            }
            catch (System.Data.SqlClient.SqlException E)
            {
                throw new Exception(E.Message);
            }
            finally
            {
                Cmd.Dispose();
                Conn.Close();
            }
        }
    }
}
